// video模块
export default {
	props: {
		// 可传视频数量
		videoLimit: {
			type: [Number, String],
			default: 9
		},
		// 拍摄时长 秒
		videoMaxDuration: {
			type: Number,
			default: 15
		},
		// 拍摄 & 相册
		videoSourceType: {
			type: Array,
			default: () => ['camera', 'album']
		},
		// 前置 or 后置 可选'front', 'back'
		videoCamera: {
			type: String,
			default: 'back'
		}
	},
	data() {
		return {
			videoShow: false,
			video: [],
			videoSourceTypeName: {
				camera: '拍摄',
				album: '从手机相册选择'
			},
			videoContext: null,
			currentVideoSrc: '',
			currentVideoId: this.$w.guid()
		}
	},
	mounted() {
		if (this.renderVideo) {
			this.videoContext = uni.createVideoContext(this.currentVideoId, this);
		}
	},
	computed: {
		showCenterPlayBtn() {
			// #ifdef MP-WEIXIN
			return false;
			// #endif
			// #ifdef APP-VUE
			return true;
			// #endif
		}
	},
	methods: {
		async addVideo() {
			if (this.disabled) return;
			if (this.video.length == this.videoLimit) { // 视频已满
				let isContinue = await this.isFull('video');
				if (!isContinue) return;
			}
			if (this.videoSourceType.length == 1) {
				this.chooseVideo(this.videoSourceType[0])
			} else {
				uni.showActionSheet({
					itemList: this.videoSourceType.map(item => this.videoSourceTypeName[item]),
					success: res => {
						this.chooseVideo(this.videoSourceType[res.tapIndex])
					}
				});
			}
		},
		chooseVideo(videoSourceType) {
			// app添加视频方法，无covor
			// #ifdef APP-VUE || H5
			uni.chooseVideo({
				sourceType: [videoSourceType],
				camera: this.videoCamera,
				maxDuration: this.videoMaxDuration,
				success: async data => {
					let header = {
						// 'Content-Type': 'multipart/form-data',
						// 'Cookie': this.$http.sessionId
					};
					if (this.$http.token)
						header[this.$http.token.header] = this.$http.token.token;
					if (this.checkToken) {
						await this.$http.checkToken(header, () => {
							uni.showToast({
								title: '登录信息已失效',
								icon: 'none'
							})
							uni.reLaunch({
								url: '/pages/login/login'
							});
							this.$http.logout();
						})
					}
					uni.showLoading({
						title: '上传中……',
						mask: true
					});
					let res = await uni.uploadFile({
						url: this.url.replace('$type', 'video'),
						filePath: data.tempFilePath,
						// name: 'file',
						header
					});
					uni.hideLoading();
					if (res[1]?.statusCode == 200) { // 请求成功
						this.video.push({
							// #ifdef APP-VUE
							previewCover: data.tempFilePath,
							// #endif
							previewSrc: data.tempFilePath,
							...JSON.parse(res[1].data).message
						})
					} else {
						uni.showToast({
							title: '上传失败',
							icon: 'none'
						});
					}
					this.change();
				},
				fail: () => {
					if (videoSourceType == 'camera') {
						this.$w.permission('camera')
						this.$w.permission('record')
					} else {
						this.$w.permission('photoLibrary')
					}
				}
			})
			// #endif

			// 微信小程序添加视频方法，有covor
			// #ifdef MP-WEIXIN
			uni.chooseMedia({
				mediaType: ['video'],
				sourceType: [videoSourceType],
				camera: this.videoCamera,
				maxDuration: this.videoMaxDuration,
				count: 1,
				success: async data => {
					let header = {
						'Content-Type': 'multipart/form-data',
						// #ifndef H5
						'Cookie': this.$http.sessionId
						// #endif
					};
					if (this.checkToken) {
						await this.$http.checkToken(header, () => {
							uni.showToast({
								title: '登录信息已失效',
								icon: 'none'
							})
							uni.reLaunch({
								url: '/pages/login/login'
							});
							this.$http.logout();
						})
					}
					uni.showLoading({
						title: '上传中……',
						mask: true
					});
					// 视频上传
					let res = await uni.uploadFile({
						url: this.url.replace('$type', 'video'),
						filePath: data.tempFiles[0].tempFilePath,
						name: 'file',
						header
					});
					uni.hideLoading();
					if (res[1]?.statusCode == 200) {
						this.video.push({
							previewCover: data.tempFiles[0].thumbTempFilePath,
							previewSrc: data.tempFiles[0].tempFilePath,
							...JSON.parse(res[1].data).message
						})
					} else {
						uni.showToast({
							title: '上传失败',
							icon: 'none'
						});
					}
					this.change();
				}
			})
			// #endif
		},
		previewVideo(src) {
			this.currentVideoSrc = src;
			this.videoShow = true;
			this.$nextTick(() => {
				// #ifndef H5
				this.videoContext.requestFullScreen({
					direction: 0
				});
				// #endif
				this.videoContext.play();
			})
		},
		// 监听全屏退出事件
		fullscreenChange(e) {
			if (!e.detail.fullScreen) {
				this.videoContext.pause();
				this.videoShow = false;
			}
		}
	}
}